// xc_lshop/integralmall/pro/infor.js
var actbill = require('../model/integralpro_contents')
var html2wxml = require('../../../html2wxml/html2wxml');
var app = getApp();

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],
  
  /**
   * 页面的初始数据
   */
  data: {
    // navopacity: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        
  },
  xcloaddata: function (options, config) {
    this.confgset(config)
    this.getmodel(options.id, 0)
  },
  buyop: function (ev) {
    var dataset = ev.currentTarget.dataset
    if (!this.data.memberinfo) {
      this.login();
      return;

    }
    console.log("this.data.xcmodel") 
    console.log(this.data.xcmodel) 

    var xcmodel=this.data.xcmodel;
    if(xcmodel.stock<=0){ 
      wx.showToast({ 
        icon:"none", 
        title: '产品已兑完 ', 
      }) 
      return; 
    }  
    if(xcmodel.guigetype==-1){
      var newmodel={};
      newmodel['activityid']=0;
      newmodel['activityprice']=-1;
      newmodel['attrnameinfo']=[];
      newmodel['attrs']=[];
      newmodel['bpid']=-1;
      newmodel['cid']=0;
      newmodel['flag']=-1;
      newmodel['id']=xcmodel['id'];
      newmodel['integral']=xcmodel['integral'];
      newmodel['keyval']=xcmodel['id']+"_0_0_-1";
      newmodel['name']=xcmodel['name'];
      newmodel['num']=1;
      newmodel['pnum']=-2;
      newmodel['price']=xcmodel['price'];
      newmodel['pro_ident']=-1;
      newmodel['realprice']=xcmodel['realprice'];
      newmodel['simg']=xcmodel['simg'];
      newmodel['type']=xcmodel['type'];
      newmodel['weight']=xcmodel['weight'];
      console.log("ppppp")
      console.log(newmodel)

      app.globalData.temcart = {};
      app.globalData.temcart["data"] = [newmodel]; 
      wx.navigateTo({
        url: '/' + app.globalData.xcsys.m + '/integralmall/paycore/payinfo?iscart=-1'
      })
      return;
    }

    var that = this
    var option = "";
    if (this.data.activityid && this.data.activitytype) {
      option = {
        aid: this.data.activityid,
        atype: this.data.activitytype
      }
    }
    if (ev.currentTarget.dataset.pid) {
      var pid = ev.currentTarget.dataset.pid;
      if (pid == 1) {
        option = {
          aid: 0,
          atype: 0
        }
      }
    }
    if (ev.currentTarget.dataset.groupid) {
      option['groupid'] = ev.currentTarget.dataset.groupid
    }
    console.log("Fffffffffff")
    console.log(that.data.id)
    console.log(that.data.selected)
    console.log(ev.currentTarget.dataset.op)
    console.log(option)


    actbill.showattrchose(that, that.data.id, that.data.selected, ev.currentTarget.dataset.op, option)
  },
  afterlogin: function (options, config, userinfo) {

  },
  confgset(config) {
    //主题设置
    var updateconfig = {};
    if (config.integral) {
      updateconfig['integralconfig'] = config.integral
    }
    if (config.order) {
      var owngroupstatus = config.order.owngroupstatus
      updateconfig['orderconfig'] = config.order
      updateconfig['owngroupstatus'] = owngroupstatus
    }
    if (config.prothem && config.prothem.themcolor) {
      var themconfig = config.prothem.themcolor.config;
      updateconfig['themconfig'] = themconfig
    }
    if (config.prothem && config.prothem.detail) {
      var detailconfig = config.prothem.detail.proconfig;  
      var customStyle = config.prothem.detail.customStyle;
      var popconfig = config.prothem.popup.proconfig;
      var popCustomStyle = config.prothem.popup.customStyle;
      updateconfig['detailconfig'] = detailconfig
      console.log("detailconfig")
      console.log(detailconfig)
      updateconfig['customStyle'] = customStyle
      updateconfig['popconfig'] = popconfig
      updateconfig['popCustomStyle'] = popCustomStyle
    }
    this.setData(updateconfig)
    //导航设置
    var commentshow = this.data.commentshow
    if (config.proconfig) {
      commentshow = config.proconfig.pro.commentshow
    }
    var detailconfig = config.prothem.detail.proconfig;
    if (detailconfig.evaluate.status == 1 || detailconfig.moreinfor.status == 1) {
      var navlist = [{
        id: "main",
        name: "商品"
      }]
      if (commentshow != "3" && detailconfig.evaluate.status == 1) {
        navlist.push({
          id: "evaluate",
          name: "评价"
        })
      }
      if (detailconfig.moreinfor.status == 1) {
        navlist.push({
          id: "infor",
          name: "详情"
        })
      }
      // navlist.push({id: "recommend",name: "推荐" })
      this.setData({
        commentshow,
        navlist
      })
    }

  },

   // 注册
   login() {
    var app = getApp()
    var that = this
    app.xc_sys_config.authop.oppower("login").then(function (res) {
      that.setData({
        "memberinfo": app.globalData.member.memberinfo
      })
    })
  },
  afterlogin: function (options, config, userinfo) {
    if (userinfo.memberinfo) {
      this.setData({
        memberinfo: userinfo.memberinfo
      })
    }
  },
  getmodel(id, activityid) {
    var that = this
    actbill.getmodel(id, activityid).then(res => {
     

      if (res.status == 1) {
      
        html2wxml.html2wxml('article', res.obj.contents, this, 5);
        var xcmodelobj = res.obj;
        var acttype = 0; //活动类型
        if (activityid && xcmodelobj['activitybrief']) {
          acttype = xcmodelobj['activitybrief']['itype']
          var actinforstock =""
          if(xcmodelobj.skudata && xcmodelobj.skudata.stockdata){
            actinforstock=xcmodelobj.skudata.stockdata;
          }
          if (xcmodelobj.guigetype == 1 && actinforstock) {
            var min = xcmodelobj.realprice;
            var sales = 0;
            actinforstock.forEach(element => {
              if (element['activityprice']) {
                if (parseInt(min) > parseInt(element['activityprice'])) {
                  min = element['activityprice'];
                }
              }
              if (element.basicesales) {
                sales += parseInt(element.basicesales);
              }
            });
            xcmodelobj['activitybrief']['sales'] = sales;
            xcmodelobj['activitybrief']['minprice'] = min;
          }
          if (xcmodelobj.activitybrief.starttime) {
            var thetime = xcmodelobj.activitybrief.starttime;
            var theendtime = xcmodelobj.activitybrief.endtime;
            var stime = new Date(Date.parse(thetime.replace(/-/g, "/")));
            var etime = new Date(Date.parse(theendtime.replace(/-/g, "/")));
            var curDate = new Date();
            var actstatus = 0;


            if (etime < curDate) {
              actstatus = 2 //活动结束 3上限 限购
            } else if (curDate < stime) { //活动未开始
              actstatus = -1;
            } else { //活动进行
              actstatus = 1
              that.autoexcel();
            }
            that.setData({
              actstatus: actstatus,
            })
          }
        }

        if (acttype == 2) {
          that.getgrouplist(id, activityid);
        }
        that.setData({
          id: id,
          activityid: activityid,
          activitytype: acttype,
          xcmodel: xcmodelobj
        })
        console.log("xcmodelobj")
        console.log(xcmodelobj)

        wx.setNavigationBarTitle({
          title: res.obj.name
        });
      }
    })
  },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 
})